package controlador;

import java.awt.Dimension;

import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 * La clase Boton se encarga de todo el manejo de captura de eventos, 
 * asi como el manejo de imagenes, que ocurrira en cada boton durante el juego.
 * Cada clase que herede de Boton debera implementar el metodo oprimir, este
 * metodo se ejecutara cuando el usuario oprima al boton.
 *
 */
public abstract class Boton extends JPanel {

	private String imagenNormal;

	private String imagenSeleccionada;

	private String imagenOprimida;

	private String imagenActual;

	private Dimension dimension;

	protected final static String path = "Imagenes/Botones/";

	protected final static String extension = "jpg";

	public Boton(String nombreBoton, int width, int height) {

		initComponents();

		this.dimension = new Dimension(width, height);
		setSize(dimension);

		imagenNormal = new String(path + nombreBoton + "_" + "normal" + "."
				+ extension);
		imagenSeleccionada = new String(path + nombreBoton + "_" + "select"
				+ "." + extension);
		imagenOprimida = new String(path + nombreBoton + "_" + "press" + "."
				+ extension);

		imagenActual = imagenNormal;

	}

	public void paintComponent(Graphics g) {
		ImageIcon imagen = new ImageIcon(imagenActual);
		g.drawImage(imagen.getImage(), 0, 0, dimension.width, dimension.height,
				null);
		super.paintComponents(g);
		setOpaque(false);
	}

	public void setImagenSeleccionada(String nombreBoton) {
		imagenSeleccionada = new String(path + nombreBoton + "_" + "select"
				+ "." + extension);
	}

	public void setImagenOprimida(String nombreBoton) {
		imagenOprimida = new String(path + nombreBoton + "_" + "press" + "."
				+ extension);
	}

	private void setImagenActual(String nombreBoton) {
		imagenActual = nombreBoton;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(java.awt.event.MouseEvent evt) {
				formMouseEntered(evt);
			}

			public void mouseExited(java.awt.event.MouseEvent evt) {
				formMouseExited(evt);
			}

			public void mousePressed(java.awt.event.MouseEvent evt) {
				formMousePressed(evt);
			}

			public void mouseReleased(java.awt.event.MouseEvent evt) {
				formMouseReleased(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 40,
				Short.MAX_VALUE));
	}// </editor-fold>//GEN-END:initComponents

	private void formMouseEntered(@SuppressWarnings("unused")
	java.awt.event.MouseEvent evt) {// GEN-FIRST:event_formMouseEntered
		setImagenActual(imagenSeleccionada);
		repaint();
	}// GEN-LAST:event_formMouseEntered

	private void formMouseExited(@SuppressWarnings("unused")
	java.awt.event.MouseEvent evt) {// GEN-FIRST:event_formMouseExited
		setImagenActual(imagenNormal);
		repaint();
	}// GEN-LAST:event_formMouseExited

	/**
	 * Codigo a ejecutar al presionar el boton.
	 */
	protected abstract void oprimir();

	private void formMousePressed(@SuppressWarnings("unused")
	java.awt.event.MouseEvent evt) {// GEN-FIRST:event_formMousePressed
		setImagenActual(imagenOprimida);
		repaint();
	}// GEN-LAST:event_formMousePressed

	private void formMouseReleased(@SuppressWarnings("unused")
	java.awt.event.MouseEvent evt) {// GEN-FIRST:event_formMouseReleased
		setImagenActual(imagenNormal);
		repaint();
		oprimir();
	}// GEN-LAST:event_formMouseReleased

	// Variables declaration - do not modify//GEN-BEGIN:variables
	// End of variables declaration//GEN-END:variables

}
